var express = require('express')
var path = require('path')
var cookieParser = require('cookie-parser')
var logger = require('morgan')
var cors = require('cors')
const { expressjwt } = require('express-jwt')

var indexRouter = require('./routes/index')
var usersRouter = require('./routes/users')
var noticesRouter = require('./routes/notices')
var eventsRouter = require('./routes/events')

var app = express()

app.use(cors())
app.use(logger('dev'))
app.use(express.json())
app.use(express.urlencoded({ extended: false }))
app.use(cookieParser())
app.use(express.static(path.join(__dirname, 'public')))

// 注册全局中间件, 除了/users/login不验证外, 其它的接口都需要验证
app.use(
	expressjwt({
		secret: 'woaini',
		algorithms: ['HS256'],
	}).unless({ path: ['/users/login'] })
)
app.use('/', indexRouter)
app.use('/users', usersRouter)
app.use('/notices', noticesRouter)
app.use('/events', eventsRouter)

module.exports = app
